package com.lehecai.ucenter.service.impl.permission;

import java.util.ArrayList;
import java.util.List;

import com.google.common.collect.Lists;
import com.lehecai.ucenter.dao.QueryParam;
import com.lehecai.ucenter.dao.permission.PermissionDao;
import com.lehecai.ucenter.dao.permission.RoleDao;
import com.lehecai.ucenter.dao.permission.RolePermissionDao;
import com.lehecai.ucenter.entity.permission.Permission;
import com.lehecai.ucenter.entity.permission.Role;
import com.lehecai.ucenter.entity.permission.RolePermission;
import com.lehecai.ucenter.service.permission.RoleService;

public class RoleServiceImpl implements RoleService {
	private RoleDao roleDao;
	private RolePermissionDao rolePermissionDao;
	private PermissionDao permissionDao;

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}
	
	public RolePermissionDao getRolePermissionDao() {
		return rolePermissionDao;
	}

	public void setRolePermissionDao(RolePermissionDao rolePermissionDao) {
		this.rolePermissionDao = rolePermissionDao;
	}

	public PermissionDao getPermissionDao() {
		return permissionDao;
	}

	public void setPermissionDao(PermissionDao permissionDao) {
		this.permissionDao = permissionDao;
	}

	@Override
	public Role get(Long id) {
		return roleDao.get(id);
	}

	@Override
	public List<Role> getAll() {
		return roleDao.getAll();
	}

	@Override
	public Role update(Role role) {
		return roleDao.merge(role);
	}

	@Override
	public void delete(Role role) {
		roleDao.delete(role);
	}

	@Override
	public List<Permission> findPermissionsByRole(Role role) {
		QueryParam queryParam = new QueryParam();
		queryParam.setParameter(RolePermission.ROLE_ID_PROPERTY_NAME, role.getId());
		List<RolePermission> rolePermissions = rolePermissionDao.findList(queryParam);
		List<Permission> permissions = new ArrayList<Permission>();
		if(rolePermissions != null && !rolePermissions.isEmpty()) {
			for(RolePermission rp : rolePermissions){
				Permission p = permissionDao.get(rp.getPermissionId());
				permissions.add(p);
			}
		}
		return permissions;
	}
	
	@Override
	public void manage(Role role, List<Permission> permissions){
		role = roleDao.merge(role);
		List<Permission> ownPermissions = findPermissionsByRole(role);
		if(ownPermissions == null){
			ownPermissions = Lists.newArrayList();
		}
		List<Permission> delPermissions = Lists.newArrayList();
		for(Permission perm :ownPermissions){
			delPermissions.add(perm);
		}
		delPermissions.removeAll(permissions);
		List<RolePermission> delRolePermissions = Lists.newArrayList();
		for(Permission perm :delPermissions){
			QueryParam queryParam = new QueryParam();
			queryParam.setParameter(RolePermission.ROLE_ID_PROPERTY_NAME, role.getId());
			queryParam.setParameter(RolePermission.PERMISSION_ID_PROPERTY_NAME, perm.getId());
			delRolePermissions.addAll(rolePermissionDao.findList(queryParam));
		}
		for(RolePermission rp : delRolePermissions){
			rolePermissionDao.delete(rp);
		}
		
		permissions.removeAll(ownPermissions);
		for(Permission perm :permissions){
			RolePermission rp = new RolePermission(role.getId(), perm.getId());
			rolePermissionDao.merge(rp);
		}
	}
}
